package cims;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

import db.DBManager;
import db.DbAccess;
import db.Record;

public class CustomerRecord extends DbAccess<CustomerRecord> implements Record<CustomerRecord> {

	private String number;		//	顧客番号
	private String name;			//	顧客名
	private String address;		//	住所
	private String tel;				//	電話番号

	public Map<String, String> getInfo(String number) {
		Connection con = null;
		PreparedStatement ps = null;
		ResultSet rs = null;

		CustomerRecord rec = new CustomerRecord();

		String sql = "select * from customers where number = ?";
		try {
			con = DBManager.getConnection();
			ps = con.prepareStatement(sql);
			ps.setString(1, number);
			rs = ps.executeQuery();

			if (rs.next()) {
				rec.ResultSetToRecord(rs);
			}
			rs.close();
		}
		catch (SQLException e) {
			rs = null;
			e.printStackTrace();
		}
		finally {
			if (ps != null) {
				try {ps.close();}
				catch (SQLException e) {e.printStackTrace();}
			}
			if (con != null) {
				try {con.close();}
				catch (SQLException e) {e.printStackTrace();}
			}
		}
		return rec.RecordToMap();
	}

	/* (非 Javadoc)
	 * @see db.DbAccess#ResultSetToRecord(java.sql.ResultSet)
	 */
	@Override
	protected Record<CustomerRecord> ResultSetToRecord(ResultSet rs) {
		try {
			super.setId(rs.getInt("id"));
			this.setNumber(rs.getString("number"));
			this.setName(rs.getString("name"));
			this.setAddress(rs.getString("address"));
			this.setTel(rs.getString("tel"));
		}
		catch (SQLException e) {
			e.printStackTrace();
		}
		return this;
	}

	/* (非 Javadoc)
	 * @see db.DbAccess#RecordToMap(db.Record)
	 */
	@Override
	protected Map<String, String> RecordToMap() {
		Map<String, String> map = new HashMap<String, String>();

		if (this.number == null) {
			map = null;
		}
		else {
			map.put("id", String.valueOf(super.getId()));
			if (this.number != null) map.put("number", this.number);
			if (this.name != null) map.put("name", this.name);
			if (this.address != null) map.put("address", this.address);
			if (this.tel != null) map.put("tel", this.tel);
		}
		return map;
	}

	/* (非 Javadoc)
	 * @see db.Record#find(int)
	 */
	@Override
	public Record<CustomerRecord> find(int id) {
		return find_by_id("customers", id);
	}

	/* (非 Javadoc)
	 * @see db.Record#add(db.Record)
	 */
	@Override
	public int add() {
		return add("customers", RecordToMap());
	}

	/* (非 Javadoc)
	 * @see db.Record#remove(int)
	 */
	@Override
	public int remove(int id) {
		return remove_by_id("customers", id);
	}

	/* (非 Javadoc)
	 * @see db.Record#update(db.Record)
	 */
	@Override
	public int update() {
		return update("customers", RecordToMap());
	}

	/**
	 * @return number
	 */
	public String getNumber() {
		return number;
	}

	/**
	 * @param number セットする number
	 */
	public void setNumber(String number) {
		this.number = number;
	}

	/**
	 * @return name
	 */
	public String getName() {
		return name;
	}

	/**
	 * @param name セットする name
	 */
	public void setName(String name) {
		this.name = name;
	}

	/**
	 * @return address
	 */
	public String getAddress() {
		return address;
	}

	/**
	 * @param address セットする address
	 */
	public void setAddress(String address) {
		this.address = address;
	}

	/**
	 * @return tel
	 */
	public String getTel() {
		return tel;
	}

	/**
	 * @param tel セットする tel
	 */
	public void setTel(String tel) {
		this.tel = tel;
	}

}
